
$('.modal').on('click','.kesn_login',function(){
	var _this = $(this);
	var _url = _this.attr('data-url');
	var name = $('input.login_name').val();
	var password = $('input.login_password').val();
	 $.ajax({
	      url : _url,
	      async:true,
	      type:'POST',
	      data:{
	      	'name':name,
	      	'password':password
	      },
	      dataType:'JSON',
	      success:function(data,textStatus,jqXHR){
	      	var _html = '';
	            if (data.err != 0) {
	            	alert('密码或用户名错误！');
	            }else if (data.err == 0) {
	            	$('#login_view').modal('hide');
	            	window.location.reload();
                }
               
	          },
	          error:function(){
	           
	          }
	    });
});

//注册界面
$('.modal').on('click','.kesn_register_view',function(){
	var _this = $(this);
	var _url = _this.attr('data-url');
	$.ajax({
	      url : _url,
	      async:true,
	      type:'GET',
	      data:{
	      },
	      dataType:'JSON',
	      success:function(data,textStatus,jqXHR){
	            if (data.err != 0) {
	            	alert(data.content);
	            }else if (data.err == 0) {
	            	var warehouse_select_html = '';
	            	var warehouse_list = data.data.warehouse;
	            	for(var key in warehouse_list){
	            		warehouse_select_html += '<option value="'+warehouse_list[key].id+'">'+warehouse_list[key].warehouse+'</option>';
	            	}
	            	var register_html = '<div class="modal-dialog" role="document">'
					    +'<div class="modal-content">'
					      +'<div class="modal-header">'
					        +'<h4 class="modal-title">用户注册</h4>'
					      +'</div>'
					      +'<div class="modal-body">'
					        +'<form class="form-horizontal">'
							  +'<div class="form-group">'
							    +'<label for="user_name" class="col-sm-2 control-label">用户名</label>'
							    +'<div class="col-sm-10">'
							      +'<input type="text" class="form-control register_name" placeholder="用户名">'
							    +'</div>'
							  +'</div>'
							  +'<div class="form-group">'
							    +'<label for="password" class="col-sm-2 control-label">密码</label>'
							    +'<div class="col-sm-10">'
							      +'<input type="password" class="form-control register_password" placeholder="密码">'
							    +'</div>'
							  +'</div>'
							  +'<div class="form-group">'
							    +'<label for="sure_password" class="col-sm-2 control-label">确认密码</label>'
							    +'<div class="col-sm-10">'
							      +'<input type="password" class="form-control register_sure_password" placeholder="确认密码">'
							    +'</div>'
							  +'</div>'
							  +'<div class="form-group">'
							    +'<label for="warehouse" class="col-sm-2 control-label">仓库名</label>'
							    +'<div class="col-sm-10">'
							      +'<select class="form-control select select-primary select-block mbl" name="register_warehouse">'
									    +warehouse_select_html
									+'</select>'
							    +'</div>'
							  +'</div>'
							+'</form>'
					      +'</div>'
					      +'<div class="modal-footer">'
					        +'<button type="button" class="btn btn-primary kesn_register" data-url="/index.php/app/main/register">注册</button>'
					      +'</div>'
					      +'<div id="alert_div"></div>'
					    +'</div>'
					  +'</div>';
					$('.modal').html(register_html);
                }
               
	          },
	          error:function(error){
	           	console.log(error);
	           	alert('网络错误！');
	          }
	    });	
});

//注册
$('.modal').on('click','.kesn_register',function(){
	var _this = $(this);
	var this_modal = _this.parents('.modal');
	var _url = _this.attr('data-url');
	var name = $.trim(this_modal.find('input.register_name').val());
	var password = $.trim($(this_modal.find('input.register_password')).val());
	var sure_password = $.trim($(this_modal.find('input.register_sure_password')).val());
	var warehouse = this_modal.find('select[name=register_warehouse]').val();
	if (password != sure_password) {
		alert('输入的密码与确认密码不一致！');
		return;
	}
	if (name == '' || password == '' || warehouse == '') {
		alert ('用户名，密码，仓库字段必填！');
	}
	$.ajax({
	      url : _url,
	      async:true,
	      type:'POST',
	      data:{
	      	'user_name':name,
	      	'password':password,
	      	'warehouse_id':warehouse
	      },
	      dataType:'JSON',
	      success:function(data,textStatus,jqXHR){
	            if (data.err != 0) {
	            	alert(data.content);
	            }else if (data.err == 0) {
	            	var _html = '<div class="alert alert-info" role="alert">'+data.content+'</div>';
	            	$('#alert_div').html(_html);
	            	setTimeout(function(){
	            		$('#login_view').modal('hide');
	            	},6000);
	            	
	            	window.location.reload();
                }
               
	          },
	          error:function(error){
	           console.log(error);
	          }
	    });

});

 $('body').keydown(function(){
    if (event.keyCode == '13') {
    	if ($('ul.login_nav').attr('data-ifLogin') == 'never') {
    		$('.kesn_login').click();
    	}
    }
});

$('.login_nav').on('click','.logout',function(){
	var con = confirm('您确定要退出登录吗？');
	if (con == false) {
		return;
	}
	var _this = $(this);
	var _url = _this.attr('data-url');
	$.ajax({
	      url : _url,
	      async:true,
	      type:'POST',
	      data:{},
	      dataType:'JSON',
	      success:function(data,textStatus,jqXHR){
	      	var _html = '';
	            if (data.err != 0) {
	            }else if (data.err == 0) {
	            	$('.user_info').remove();
	            	$('.logout').remove();
	            	$('.never_read').remove();
	            	var _html = '<li class="login_link" data-toggle="modal" data-target="#login_view"><a href="#">登录</a></li>';
	            	$('ul.login_nav').append(_html);     
                }
               
	          },
	          error:function(){
	           
	          }
	    });
});
//检查是否登录
var login_status = $('ul.login_nav').attr('data-ifLogin');
if (login_status == 'never') {
	$('.force_login').click();
}


function kesn_prompt(type,message,_function = function(){}){
		type = $.trim(type);
		var _type = '';
		if(type == 'success'){
			_type = 'alert-success';
		}else if(type == 'fail'){
			_type = 'alert-danger';
		}
		var _kesn_prompt = $(".kesn_prompt");
		if (_kesn_prompt.hasClass('alert-success')) {
			_kesn_prompt.removeClass('alert-success');
		}
		if (_kesn_prompt.hasClass('alert-danger')) {
			_kesn_prompt.removeClass('alert-danger');
		}
		_kesn_prompt.addClass(_type);
		_kesn_prompt.text(message);
		_kesn_prompt.fadeToggle(4000,_function);
	}